package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.StringUtil;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.convert.MerchantConvert;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MechantServiceImpl implements MerchantService {
    @Autowired
    MerchantMapper merchantMapper;

    //创建商户
    @Override
    @Transactional
    public MerchantDTO creatMerchant(MerchantDTO merchantDTO) throws BusinessException {
        //非空校验
        if (merchantDTO == null){
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        //手机号非空校验
        if (StringUtil.isBlank(merchantDTO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        //联系人非空校验
        if (StringUtil.isBlank(merchantDTO.getUsername())){
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        //密码非空校验
        if (StringUtil.isBlank(merchantDTO.getPassword())){
            throw new BusinessException((CommonErrorCode.E_100111));
        }
        //唯一性检验
        LambdaQueryWrapper<Merchant> lambdaQueryWrapper = new LambdaQueryWrapper<Merchant>().eq(Merchant::getMobile,merchantDTO.getMobile());
        Integer count = merchantMapper.selectCount(lambdaQueryWrapper);
        if (count > 0){
            throw new BusinessException(CommonErrorCode.E_100113);
        }


        Merchant merchant = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
        //0代表未申请
        merchant.setAuditStatus("0");
        merchantMapper.insert(merchant);
        MerchantDTO merchantDTONew = MerchantConvert.INSTANCE.entity2dto(merchant);
        return merchantDTONew;
    }

    /**
     * 根据ID查商户的信息
     * @param merchantId
     * @return
     * @throws com.shanjupay.common.domain.BusinessException
     */
    @Override
    public MerchantDTO queryMechantByID(Long merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        MerchantDTO merchantDTO = new MerchantDTO();
        merchantDTO.setId(merchant.getId());
        return merchantDTO;
    }
    @Transactional
    @Override
    public void applyMerchant(Long merchantId, MerchantDTO merchantDTO) throws BusinessException {
        if(merchantDTO == null || merchantId == null){
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        Merchant merchant_new = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
        merchant_new.setAuditStatus("1");
        merchant_new.setTenantId(merchant.getTenantId());
        merchantMapper.updateById(merchant_new);
    }
}
